<?php
/**
 * Created by PhpStorm.
 * User: serafina
 * Date: 2015/9/23
 * Time: 09:50
 */

function mobile_service_stock_info_list($parameters){
    global $user;
    $query = db_select('stock_info', 'S');//库存表
    $query->join('distributor', 'D', 'S.distributor_id=D.distributor_id');//内连接 成一个表
    $query->join('product_sku', 'V', 'V.product_sku_id=S.sacfvariety_id');//通过distributor addr_id 左联addr 得到地址名称
    $query->fields('S');
    $query->addField('V', 'variety_name');
    $query->addField('V', 'proportion');
    $query->addField('V', 'SM');
    $query->addField('D', 'distributor_name');
//    if(!user_access('view all stock_info') && user_access('view own stock_info')){
//        $query->condition('S.author_uid',$user->uid, '=');
//    }
    $form_state = [];
    $form_state['values']['employee_list'] == 'default';
    salesman_distributor_role_relate_query_info($form_state,$query,'view all stock_info','view own stock_info',"S");
    //条件筛选
    if (!empty($parameters['year'])) {//year查询条件
        $query->condition('S.year', $parameters['year']);
    }
    if (!empty($parameters['month'])) {//month查询条件
        $query->condition('S.month', $parameters['month']);
    }
    if (!empty($parameters['sacfvariety'])) {
        $query->condition('S.sacfvariety_id', $parameters['sacfvariety']);
    }

    if (!empty($parameters['employee_list'])) {//查询条件
        if ($parameters['employee_list']) {
            $query->condition('S.author_uid', $parameters['employee_list']);
        }
    }

    $query->orderBy('S.stock_info_id', 'DESC');
    if (!empty($parameters['cursor']) && !empty($parameters['comparator'])) { //游标翻页
        $query->condition('S.stock_info_id', $parameters['cursor'], $parameters['comparator']);
    }
    $query->range(0,10);

    $results = $query->execute();
    $stock_info_list = [];
    if(!empty($results)){
        foreach ($results as $result) {
            $stock_info_list[] = $result;
        }
    }
    $object_stock_info_list = (object)["data"=>$stock_info_list];
    return  $object_stock_info_list;
}

function mobile_service_stock_info_add($parameters){
    global $user;
    $module_name = "stock_info";
    $id = !empty($parameters['stock_info_id'])?$parameters['stock_info_id']:'';
    if (empty($id)) { //add new brand_sales
        if(!user_access('add '.$module_name)){
            return ['option'=>0,'errorNum'=>3];
        }
        $nid = db_insert('stock_info') // Table name no longer needs {}
        ->fields(array(
            'distributor_id' => $parameters['distributor_id'],
            'sacfvariety_id' => $parameters['sacfvariety_id'],
            'year' => $parameters['year'],
            'month' => $parameters['month'],
            'last_month_stock' => $parameters['last_month_stock'],
            'return_stock' => $parameters['return_stock'],
            'this_month_instock' => $parameters['this_month_instock'],
            'this_month_outstock' =>$parameters['this_month_outstock'],
            'this_month_stock' => $parameters['this_month_stock'],
            'this_month_sales' => $parameters['this_month_sales'],
            'details' => $parameters['details'],
            'author_uid' => $user->uid,
            'timestamp' => time(),))
            ->execute();
            $result_option = (object)["option" => 1];
            return $result_option;
    } else {
        if(!mobile_user_edit_access($module_name,$module_name.'_id',$id,'edit own '.$module_name,'edit all '.$module_name)){
            return ['option'=>0,'errorNum'=>3];
        }
        $nid = db_update('stock_info')
        ->fields(array(
            'distributor_id' => $parameters['distributor_id'],
            'sacfvariety_id' => $parameters['sacfvariety_id'],
            'year' => $parameters['year'],
            'month' => $parameters['month'],
            'last_month_stock' => $parameters['last_month_stock'],
            'return_stock' => $parameters['return_stock'],
            'this_month_instock' => $parameters['this_month_instock'],
            'this_month_outstock' => $parameters['this_month_outstock'],
            'this_month_stock' => $parameters['this_month_stock'],
            'this_month_sales' => $parameters['this_month_sales'],
            'details' => $parameters['details'],
            'timestamp' => time(),))
            -> condition('stock_info_id',$id, '=')
            ->execute();
        if($nid > 0){
            $result_option = (object)["option" => 1];
            return $result_option;
        }
        $result_option = (object)["option" => 0];
        return $result_option;
    }
}

function mobile_service_stock_info_stats_list($parameters){
    $start  = $parameters['page_count'] * 10;
    $geo_level = $parameters['geo_level'];
    if ($geo_level == '0') {
        $join_column = 'country_id';
    }else if ($geo_level == '1') {
        $join_column = 'province_id';
    } else if ($geo_level == '2') {
        $join_column = 'city_id';
    } else if ($geo_level == '3') {
        $join_column = 'county_id';
    } else if ($geo_level == '4') {
        $join_column = 'town_id';
    } else {
        $join_column = 'village_id';
    }
    $map_or_zone = "map";
    $juris = $parameters['juris'];
    $salesman = $parameters['salesman'];
    $distributor = $parameters['distributor'];

    $sub_query = db_select('years', 'Y');
    $sub_query->join('months', 'M');
    $sub_query->condition('Y.year', $parameters['year']);
    $sub_query->condition('M.month', $parameters['month']);
    $sub_query->addField('Y', 'year');
    $sub_query->addField('M', 'month');

    $query = db_select('distributor', 'D');

    if ($map_or_zone == 'map') {
        $query->join('addr', 'A1', 'A1.addr_id=D.addr_id');
        if ($geo_level <= 2) {
            $query->join('addr', 'A2', "A2.addr_id=A1.$join_column");
        } else {
            $query->join('addr', 'A2', "A2.addr_id=A1.$join_column");
            $query->join('addr', 'A3', "A3.addr_id=A2.pid");
        }
    } else {
        if (!empty($distributor)) {
            // join nothing
        } else if (!empty($salesman)) {
            $query->join('salesman_distributor_map', 'SD', 'SD.distributor_id = D.distributor_id');
        } else if (!empty($juris)) {
            $query->join('salesman_distributor_map', 'SD', 'SD.distributor_id = D.distributor_id');
            $query->join('employee', 'E', 'E.employee_id = SD.salesman_id');
        }
    }
    $query->join($sub_query, 'T');
    $query->leftJoin('stock_info', 'S', 'S.year=T.year and S.month=T.month and S.distributor_id=D.distributor_id');
    $query->leftJoin('product_sku', 'V', 'V.product_sku_id=S.sacfvariety_id');

    if ($map_or_zone == 'map') {
        if ($geo_level <= 2) {
            $query->condition('A2.national_id', $parameters['national_id']);
        } else {
            $query->condition('A3.national_id', $parameters['national_id']);
            $query->condition('A2.name', $parameters['geo_name']);
        }
    } else {
        if (!empty($distributor)) {
            $query->condition('D.distributor_id', $distributor);
        } else if (!empty($salesman)) {
            $query->condition('SD.salesman_id', $salesman);
        } else if (!empty($juris)) {
            $query->condition('E.juris_id', $juris);
        }
    }

    $query->addField('D', 'distributor_id');
    $query->addField('D', 'distributor_name');
    $query->addField('T', 'year');
    $query->addField('T', 'month');
    $query->addField('V', 'product_sku_id');
    $query->addField('V', 'variety_name');
    $query->addField('V', 'proportion');
    $query->addField('V', 'SM');
    $query->addField('S', 'sacfvariety_id');
    $query->addField('S', 'last_month_stock');
    $query->addField('S', 'return_stock');
    $query->addField('S', 'this_month_instock');
    $query->addField('S', 'this_month_outstock');
    $query->addField('S', 'this_month_stock');
    $query->addField('S', 'this_month_sales');
    $query->orderBy('S.timestamp', 'DESC');
    $query->range($start,10);
    $results = $query->execute();
    $stock_info_stats_list = [];
    if(!empty($results)){
        foreach ($results as $result) {
            $stock_info_stats_list[] = $result;
        }
    }
    $object_stock_info_stats_list = (object)["data"=>$stock_info_stats_list];
    return  $object_stock_info_stats_list;
}